<?php


namespace app\admin\model;

use think\Exception;
use app\sys\model\Dict;
use app\sys\model\UrlSourceLog;
use think\Db;
use think\Model;

class UserPvLog extends Model
{
    protected $autoWriteTimestamp = true;


    public function setTitleAttr($value)
    {
        if (strlen($value) > 255) {
            return mb_substr($value, 254);
        }
        return $value;
    }

    public function setImgAttr($value)
    {
        if (strlen($value) > 255) {
            return "";
        }
        return $value;
    }

    public function setUserIconAttr($value)
    {
        if (strlen($value) > 255) {
            return "";
        }
        return $value;
    }

    /**
     * @param $data
     * @return bool
     * 添加记录
     */
    public static function addLog($data)
    {

        $userPvLog = new UserPvLog;
        $data["pxs_id"] = $data["id"];
        unset($data["id"]);
        $res = $userPvLog->allowField(true)->save($data);
        return $res;
    }

    /**
     * @param $user_no
     * @param $chat_id
     * @return array
     * 列表数据
     */
    public static function logList($user_no, $chat_id)
    {
//        $resChat = Chat::where("id",$chat_id)->where("user_no",$user_no)->find();
        $logSql = UserPvLog::where("user_no", $user_no);
        $logList = $logSql->order("create_time desc")->select();
        $logC = $logSql->count();

        return listDate($logList, $logC);
    }

    /**
     * 访客列表
     * @param $mobile
     * @param $site
     * @param $sourceName
     * @param array $date_arr
     * @param string $no
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function vList($mobile,$site,$sourceName,$date_arr=[],$no = '',$page = 0,$limit = 10)
    {
        $resPvSql = UserPvLog::field("create_time,user_no,address,pxs_id,mobile")
            ->group("user_no");

        if($mobile){
            $resPvSql->where("mobile",$mobile);
        }

        $resPvSql = $resPvSql->order("id desc")
            ->buildSql();

        $resUrSql = UrlSourceLog::field("id,no,be_url,af_url,source_name,site,create_time,address");

        if ($date_arr) {
            $resUrSql->whereBetween("create_time", [strtotime($date_arr[0]),strtotime($date_arr[1])+3600*24]);
        }

        if($site){
            $sited = Dict::getLabel("site",$site);
            $resUrSql->where("site",$sited);
        }

        if($sourceName){
            $sourceName = Dict::getLabel("source",$sourceName);
            $resUrSql->where("source_name",$sourceName);
        }

        if($no){
            $resUrSql->where("no", $no);
        }
        $resUrSql = $resUrSql->group("no")->order("id desc")
            ->page($page,$limit)
            ->buildSql();

        $resSql = Db::table($resUrSql)->alias("s")
            ->field("s.create_time,s.no as user_no,s.address,s.site,s.source_name,s.be_url,s.af_url")
            //->leftJoin($resPvSql." l","s.no = l.user_no")
        ;

        $resUrSql1 = UrlSourceLog::field("*");
        $resC = $resUrSql1->group("no")->count();

        $res = $resSql->order("s.id desc")->select();
        return listDate($res,$resC);
    }
}